package com.ygqh.baby.service.impl;

import java.util.*;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.model.WholesaleSkuModel;
import com.ygqh.baby.po.YgWholesaler;
import com.ygqh.baby.po.YgWholesalerPrebillRecordDetail;
import com.ygqh.baby.po.YgWholesalerSku;
import com.ygqh.baby.mapper.YgWholesalerSkuMapper;
import com.ygqh.baby.service.YgWholesalerService;
import com.ygqh.baby.service.YgWholesalerSkuService;
import com.ygqh.baby.template.AbstractSplitListTemplate;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;

import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.ygqh.baby.po.YgWholesalerSkuExample;
import com.ygqh.baby.ao.DataStatus;

/**
 * @author Ss
 */
@Service
public class YgWholesalerSkuServiceImpl implements YgWholesalerSkuService {

	@Autowired
	private YgWholesalerSkuMapper ygWholesalerSkuMapper;
	@Autowired
	private YgWholesalerService ygWholesalerService;

	@Override
	public List<YgWholesalerSku> find() {
		YgWholesalerSkuExample example = new YgWholesalerSkuExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygWholesalerSkuMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgWholesalerSkuExample example = new YgWholesalerSkuExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygWholesalerSkuMapper.countByExample(example);
	}

	@Override
	public YgWholesalerSku findById(Long id) {
		return ygWholesalerSkuMapper.selectByPrimaryKey(id);
	}


	@Override
	public int save(YgWholesalerSku ygWholesalerSku) {
		return ygWholesalerSkuMapper.insertSelective(ygWholesalerSku);
	}

	@Override
	public int update(YgWholesalerSku ygWholesalerSku) {
		return ygWholesalerSkuMapper.updateByPrimaryKeySelective(ygWholesalerSku);
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgWholesalerSkuExample example = new YgWholesalerSkuExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgWholesalerSku record = new YgWholesalerSku();
		record.setStatus(DataStatus.Delete);
		return ygWholesalerSkuMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgWholesalerSku ygWholesalerSku) {
		int r;
		if (ygWholesalerSku.getId() != null) {
			r = this.update(ygWholesalerSku);
		} else {
			r = this.save(ygWholesalerSku);
		}
		return r;
	}

	@Override
	public void saveWholesalePrice(List<YgWholesalerSku> list, Long wholesalerId, String handler) {
		this.validateParamForSavePrice(list, wholesalerId);
		List<String> skuCodeList = list.stream().map(YgWholesalerSku::getSkuCode).collect(Collectors.toList());
		List<YgWholesalerSku> listByExists = this.findBySkuCode(skuCodeList, wholesalerId);
		if (CollectionUtils.isEmpty(listByExists)) {
			listByExists = new ArrayList<>();
		}
		Map<String, List<YgWholesalerSku>> collect = listByExists.stream().collect(Collectors.groupingBy(YgWholesalerSku::getSkuCode));
		Date now = new Date();
		List<YgWholesalerSku> insertList = new ArrayList<>();
		List<YgWholesalerSku> updateList = new ArrayList<>();
		list.forEach(record -> {
			record.setUpdateBy(handler);
			record.setUpdateTime(now);
			if (MapUtils.isNotEmpty(collect) && collect.containsKey(record.getSkuCode())) {
				YgWholesalerSku recordFromDB = collect.get(record.getSkuCode()).get(0);
				record.setId(recordFromDB.getId());
				updateList.add(record);
			} else {
				record.setCreateTime(now);
				record.setCreateBy(handler);
				insertList.add(record);
			}
		});
		this.saveBatch(insertList);
		this.updateBatch(updateList);
	}

	@Override
	public List<YgWholesalerSku> findBySkuCode(List<String> skuCodeList, Long wholesalerId) {
		Assert.notNull(wholesalerId, "批发商id不能为空");
		return ygWholesalerSkuMapper.selectListBySkuCode(skuCodeList, wholesalerId);
	}

	@Override
	public ResultSet<WholesaleSkuModel> search(QueryInfo queryInfo, String wholesalerName, String productName) {
		int count = ygWholesalerSkuMapper.countSuper(wholesalerName, productName);
		if (count == 0) {
			return new ResultSet<>();
		}
		List<WholesaleSkuModel> list = ygWholesalerSkuMapper.selectSuper(queryInfo, wholesalerName, productName);
		return new ResultSet<>(count, list);
	}

	@Override
	public List<YgWholesalerSku> findBySkuCodeAndWholesaleName(List<String> skuCodeList, List<String> wholesalerNameList) {
		if (CollectionUtils.isEmpty(skuCodeList) && CollectionUtils.isEmpty(wholesalerNameList)) {
			return new ArrayList<>();
		}
		return ygWholesalerSkuMapper.selectListBySkuCodeAndWholesalerName(skuCodeList, wholesalerNameList);
	}

	private void validateParamForSavePrice(List<YgWholesalerSku> list, Long wholesalerId) {
		Assert.notNull(wholesalerId, "批发商id不能为空");
		YgWholesaler wholesaler = ygWholesalerService.findById(wholesalerId);
		Assert.notNull(wholesaler, "批发商不存在");
		Assert.notEmpty(list, "SKU批发价信息不能为空");
		for (YgWholesalerSku sku : list) {
			Assert.isTrue(StringUtils.isNotBlank(sku.getSkuCode()), "SKU编码不能为空");
			Assert.notNull(sku.getWholesalePrice(), "SKU批发价不能为空");
			sku.setWholesalerId(wholesalerId);
			sku.setWholesalerName(wholesaler.getWholesalerName());
		}
	}

	private void updateBatch(List<YgWholesalerSku> updateList) {
		new AbstractSplitListTemplate<YgWholesalerSku>(updateList) {
			@Override
			public int execute(List<YgWholesalerSku> recordList) {
				return ygWholesalerSkuMapper.updateBatch(recordList);
			}
		}.split();
	}

	private void saveBatch(List<YgWholesalerSku> insertList) {
		new AbstractSplitListTemplate<YgWholesalerSku>(insertList) {
			@Override
			public int execute(List<YgWholesalerSku> recordList) {
				return ygWholesalerSkuMapper.insertBatch(recordList);
			}
		}.split();
	}

}

